'use strict';

angular.module('CBO').controller('ContainerController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication','usbPort', 'EntityService', 'BarcodeEnum',
    function($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication, usbPort, EntityService, BarcodeEnum) {
        //$scope.authentication = Authentication;
        var oldUniqueID = '';
        
        $scope.btnSave = function(){
            if($scope.Module.Container.FocusedRecord.Code == undefined || 
                $scope.Module.Container.FocusedRecord.Code.trim() == ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.Container.FocusedRecord.Name == undefined || 
                $scope.Module.Container.FocusedRecord.Name.trim() == ""){
                PublicFunction.alert("名称不能为空！", "保存错误");
                return;
            }

            $scope.Module.Container.save(function(record){
                PublicFunction.tip('保存成功', '保存提示');
                if($scope.Module.Container.FocusedRecord.UniqueID != oldUniqueID){
                    $scope.Module.BarcodeMaster.Records = [];
                    $scope.Module.BarcodeMaster.add();
                    $scope.Module.BarcodeMaster.FocusedRecord.Barcode = $scope.Module.Container.FocusedRecord.UniqueID;
                    $scope.Module.BarcodeMaster.FocusedRecord.BarcodeType = BarcodeEnum.BarcodeType.Container.Value;
                    $scope.Module.BarcodeMaster.FocusedRecord.EntityID = $scope.Module.Container.FocusedRecord._id;
                    $scope.Module.BarcodeMaster.save(function(barcodeMasterRecord){
                        PublicFunction.tip('创建条码成功', '保存提示');
                    });
                }
            });
        };
        $scope.btnCancel = function(){
            $scope.Module.Container.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.Container.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.Container.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/Container/List');
        };

        usbPort.start();
        $scope.$on('$destroy', function() {
            usbPort.stop();
        });
        $scope.$on('usbPort.input', function(e, inputStr) {
            $scope.Module.Container.FocusedRecord.UniqueID = inputStr;
            $scope.$apply();
        });

        //初始化
        PublicModule.initModule($scope, 'Container');
        PublicModule.initModule($scope, 'BarcodeMaster');
        $scope.Module.Container.AfterOnAdd = function(record){
            record.IsEffective = true;
        };

        if($stateParams.id) {
            $scope.Module.Container.findById($stateParams.id, function(record){
                $scope.Module.Container.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        };

        $scope.$watch(
            function(){
                return $scope.Module.Container.FocusedRecord._id;
            },
            function(newValue, oldValue){
                if (newValue != oldValue) {
                    oldUniqueID = $scope.Module.Container.FocusedRecord.UniqueID;
                }
            }
        );
    }
]);
